package com.zlxtk.ling.nao;

import com.zlxtk.ling.po.Jue;
import com.zlxtk.ling.qiGuan.QiGuanType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 输入中枢
 * 概念：脊椎
 * 每个大脑只有一个输入中枢，
 */
public class InputZhongShu {

    private static InputZhongShu inputZhongShu;

    private Map<String, List<Jue>> data;

    public static InputZhongShu getInstance() {
        if (inputZhongShu == null) {
            inputZhongShu = new InputZhongShu();
            inputZhongShu.init();
        }
        return inputZhongShu;
    }

    private void init() {
        if (data == null) {
            data = new HashMap<String, List<Jue>>();
            data.put(QiGuanType.SHI.getDataType(), new ArrayList<Jue>());
            data.put(QiGuanType.TING.getDataType(), new ArrayList<Jue>());
            data.put(QiGuanType.XIU.getDataType(), new ArrayList<Jue>());
            data.put(QiGuanType.WEI.getDataType(), new ArrayList<Jue>());
            data.put(QiGuanType.CHU.getDataType(), new ArrayList<Jue>());
            data.put(QiGuanType.GAN.getDataType(), new ArrayList<Jue>());
        }
    }

    public void update(Jue jue) {
        List<Jue> list = data.get(jue.getQiGuanType().getDataType());
        //如果已存在来着该器官的数据，则更新；如果不存在,则新增
        synchronized (jue.getFromQiGuanId()) {
            boolean has = false;
            for (int i=0;i<list.size();i++){
                Jue o=list.get(i);
                if (o.getFromQiGuanId().equals(jue.getFromQiGuanId())) {
                    o.setData(jue.getData());
                    has = true;
                    break;
                }
            }
            if (!has) {
                list.add(jue);
            }
        }
        System.out.println("inputzhongshu----"+list.get(0).getData().getData());


    }

    public List<Jue> getJueListByQiguanType(QiGuanType qiGuanType){
        return data.get(qiGuanType.getDataType());
    }

}
